package com.shilan.administrator.qintfm.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.shilan.administrator.qintfm.R;
import com.shilan.administrator.qintfm.beans.AnchorResult;
import com.shilan.administrator.qintfm.volley.VolleySingleton;

import java.util.List;

/**
 * Created by dllo on 16/1/21.
 */
public class AnchorAdapter extends RecyclerView.Adapter<
        AnchorAdapter.AnchorViewHolder> {

    private Context context;
    private ImageLoader imageLoader;
    private List<AnchorResult.ListEntity> anchorListEntity;

    public AnchorAdapter(Context context) {
        this.context = context;
        imageLoader = VolleySingleton.getInstance()._getImageLoader();
    }

    // 获得anchorListEntity集合
    public void addAnthorData(List<AnchorResult.ListEntity> anchorListEntity) {
        this.anchorListEntity = anchorListEntity;

        notifyDataSetChanged();
    }

    @Override
    public AnchorViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context)
                .inflate(R.layout.anchor_adapter_item, parent, false);

        return new AnchorViewHolder(view);
    }



    @Override
    public void onBindViewHolder(AnchorViewHolder holder, int position) {
        if (anchorListEntity != null && anchorListEntity.size() > 0) {
            holder.titleTv.setText(anchorListEntity.get(position).getTitle());

            List<AnchorResult.ListEntity.ListEntityList> anchorList =
                    anchorListEntity.get(position).getListEntity();
            if (anchorList != null && anchorList.size() > 0) {
                holder.firstSubTv.setText(anchorList.get(0).getNickname());
                holder.firstAnchorIv.setImageUrl(anchorList.get(0).getSmallLogo(), imageLoader);
                holder.secondSubTv.setText(anchorList.get(1).getNickname());
                holder.secondAnchorIv.setImageUrl(anchorList.get(1).getSmallLogo(), imageLoader);
                holder.thirdSubTv.setText(anchorList.get(2).getNickname());
                holder.thirdAnchorIv.setImageUrl(anchorList.get(2).getSmallLogo(), imageLoader);

            }
        }
    }

    @Override
    public int getItemCount() {
        return anchorListEntity != null && anchorListEntity.size() > 0 ?
                anchorListEntity.size() : 0;
    }


    public class AnchorViewHolder extends RecyclerView.ViewHolder {
        private NetworkImageView firstAnchorIv, secondAnchorIv, thirdAnchorIv;
        private ImageView firstFollowIv, secondFollowIv, thirdFollowIv;
        private TextView titleTv, firstSubTv, secondSubTv, thirdSubTv;


        public AnchorViewHolder(View itemView) {
            super(itemView);

            firstAnchorIv = (NetworkImageView) itemView.findViewById(R.id.ivFirst_anchor);
            secondAnchorIv = (NetworkImageView) itemView.findViewById(R.id.ivSecond_anchor);
            thirdAnchorIv = (NetworkImageView) itemView.findViewById(R.id.ivThird_anchor);
            firstFollowIv = (ImageView) itemView.findViewById(R.id.ivFirst_follow_anchor);
            secondFollowIv = (ImageView) itemView.findViewById(R.id.ivSecond_follow_anchor);
            thirdFollowIv = (ImageView) itemView.findViewById(R.id.ivThird_follow_anchor);
            titleTv = (TextView) itemView.findViewById(R.id.tv_title_anchor);
            firstSubTv = (TextView) itemView.findViewById(R.id.tvFirst_subTitle_anchor);
            secondSubTv = (TextView) itemView.findViewById(R.id.tvSecond_subTitle_anchor);
            thirdSubTv = (TextView) itemView.findViewById(R.id.tvThird_subTitle_anchor);
        }
    }
}
